﻿using System;
using System.Linq;
using System.Web.Mvc;
using CVsharp.DWZMvc.Models;
using CVsharp.MVClib.Log;

namespace CVsharp.DWZMvc.Controllers
{
    public class DrugStoreManageController : Controller
    {

        public yjjDBEntities db = new yjjDBEntities();

        //
        // GET: /DrugStoreManage/

        public ActionResult Index()
        {
            ViewBag.pageNum = 1;
            ViewBag.numPerPage = 20;
            ViewBag.keywords = "";
            ViewBag.totalnum = db.DSA_DrugStore.Count();
            ViewBag.opentime = "";
            var model = db.DSA_DrugStore.OrderBy(m => m.DrugStoreID).Take(20);
            return View(model);
        }

        //
        // Post: /DrugStoreManage/
        [HttpPost]
        public ActionResult Index(int? pagenum, int? numperpage, string keyword, string keywords, string opentime, string opentimes)
        {
            if (pagenum == null) pagenum = 1;
            if (numperpage == null) numperpage = 20;
            keyword = keyword ?? "";
            ViewBag.keywords = keyword = (keyword == "" && keywords != null) ? keywords : keyword;
            opentime = opentime ?? "";
            ViewBag.opentime = opentime = (opentime == ""&&opentimes!=null) ? opentimes : opentime;
            ViewBag.pageNum = pagenum.Value;
            ViewBag.numPerPage = numperpage.Value;
            int skipnum = (pagenum.Value - 1) * numperpage.Value;
            DateTime d = opentime==""?DateTime.Now:Convert.ToDateTime(opentime);
            IQueryable<DSA_DrugStore> model = db.DSA_DrugStore
                .Where(m => (keyword == "" || m.Name.Contains(keyword) || m.Address.Contains(keyword))
                            && (opentime == "" || m.GreateDate == d));            
            ViewBag.totalnum = model.Count();
            model=model.OrderBy(m => m.DrugStoreID)
                    .Skip(skipnum)
                    .Take(numperpage.Value);
            return View(model);
        }


        //
        // GET: /DrugStoreManage/Details/5

        public ActionResult Details(int id)
        {
            db.DSA_DrugStore.Where(m => m.DrugStoreID == id).FirstOrDefault();
            return View();
        }

        //
        // GET: /DrugStoreManage/Create
        
        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /DrugStoreManage/Create
        [Log(LogContent = "新建药房")]
        [HttpPost]
        public ActionResult Create(DSA_DrugStore d)
        {
            if (ModelState.IsValid)
            {
                db.AddToDSA_DrugStore(d);
                db.SaveChanges();
                var reslut = new
                {
                    statusCode = "200",
                    message = "添加成功",
                    navTabId = "page-drugstoremanage",
                    callbackType = "closeCurrent"
                };
                return Json(reslut);
            }
            else
            {
                var reslut = new
                {
                    statusCode = "300",
                    message = "修改失败"
                };
                return Json(reslut);
            }
        }
        
        //
        // GET: /DrugStoreManage/Edit/5
 
        public ActionResult Edit(int id)
        {
            //ViewBag.method = m;
            var model = db.DSA_DrugStore.Where(a => a.DrugStoreID == id).FirstOrDefault();
            return View(model);
        }

        //
        // POST: /DrugStoreManage/Edit/5
        [Log(LogContent = "编辑药房")]
        [HttpPost]
        public ActionResult Edit(DSA_DrugStore d)
        {
            if (ModelState.IsValid)
            {
                //由于页面没有lastgradedate数据，所以要提取原数据
                int id = d.DrugStoreID;
                DSA_DrugStore o = db.DSA_DrugStore.Where(m => m.DrugStoreID == id).FirstOrDefault();
                o.Address = d.Address;
                o.Name = d.Name;
                o.ResponsiblePerson = d.ResponsiblePerson;
                o.GreateDate = d.GreateDate;

                db.SaveChanges();
                var reslut=new
                {
                    statusCode = "200",
                    message = "修改成功",
                    navTabId = "page-drugstoremanage"
                };
                return Json(reslut);
            }
            else {
                //string message = "{\"statusCode\":\"300\", \"message\":\"修改失败\"}";
                var reslut = new
                {
                    statusCode = "300",
                    message = "修改失败"    
                };
                return Json(reslut);
                }
        }
        [Log(LogContent = "删除药房")]
        [HttpPost]
        public ActionResult Delete(int[] ids,int? id)
        {
            if (id != null)   //如果是单个删除
            {
                DSA_DrugStore d = db.DSA_DrugStore.First(m => m.DrugStoreID == id.Value);
                if (d != null)
                {
                    db.DSA_DrugStore.DeleteObject(d);
                    db.SaveChanges();
                    if (db.DSA_DrugStore.Where(m => m.DrugStoreID == id.Value).FirstOrDefault() == null)
                    {
                        var reslut = new
                        {
                            statusCode = "200",
                            message = "删除成功",
                            navTabId = "page-drugstoremanage",
                        };
                        return Json(reslut);
                    }
                    else
                    {
                        var reslut = new
                        {
                            statusCode = "300",
                            message = "删除失败"
                        };
                        return Json(reslut);
                    }
                }
                return Json(new { statusCode = "300", message = "未知数据" });
            }
            else        //批量删除
            {
                bool flag = false;
                if (ids.Count() > 0)
                {
                    foreach (int i in ids)
                    {
                        DSA_DrugStore d = db.DSA_DrugStore.Where(m => m.DrugStoreID == i).First();
                        if (d != null) db.DSA_DrugStore.DeleteObject(d);
                    }
                    db.SaveChanges();
                    flag = true;
                }
                if (flag)
                {
                    var reslut = new
                    {
                        statusCode = "200",
                        message = "删除成功",
                        navTabId = "page-drugstoremanage",
                    };
                    return Json(reslut);
                }
                else    
                {
                    var reslut = new
                    {
                        statusCode = "300",
                        message = "未更改"
                    };
                    return Json(reslut);
                }
            }
        }
    }
}
